package com.brspace.qualitycontrol.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brspace.qualitycontrol.entity.*;
import com.brspace.qualitycontrol.entity.Process;
import com.brspace.qualitycontrol.entity.vo.*;
import com.brspace.qualitycontrol.mapper.*;
import com.brspace.qualitycontrol.service.LeaveService;
import com.brspace.qualitycontrol.utils.EmptyUtil;
import com.brspace.qualitycontrol.utils.R;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;

/**
 * <p>
 * 请假 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-02
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leaveing> implements LeaveService {
    @Resource
    private LeaveMapper leaveMapper;
    @Resource
    private ProcessNodeMapper nodeMapper;
    @Resource
    private ProcessMapper processMapper;
    @Resource
    private ProcessRouterMapper routerMapper;
    @Resource
    private NodeUserMapper nodeUserMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private HandleMapper handleMapper;

    /**
     * 流程审批
     * userName : 下一节点办理人  uid ： 当前节点办理人
     *
     * @Author: wangt
     * @Date: 2021-04-18 16:35:26
     */
    @Override
    public R examine(Leaveing leaveing, String userName, String uid) {
        if (leaveing.getNodeId() == 0) {
            throw new SecurityException("流程已结束！");
        }
        System.err.println("条件----------------------------------------------------------------------------------"+leaveing.getProgress());
        if ("驳回".equals(leaveing.getProgress())) {
            if ("undefined".equals(userName) || userName==null || "null".equals(userName)){
                Handle handleList = handleMapper.selectOne(new QueryWrapper<Handle>().eq("middleman_id", uid).eq("document_id", leaveing.getId()).eq("n_id",leaveing.getNodeId()));
                userName=handleList.getHandleId();
            }
        }
        // 根据当前节点获取当前流程下的PID以及NodeName
        ProcessNode processNode = nodeMapper.selectOne(new QueryWrapper<ProcessNode>().eq("id", leaveing.getNodeId()));
        // 获取初始节点的ID
        int findId = nodeMapper.selectOne(new QueryWrapper<ProcessNode>().eq("p_id", processNode.getPId()).eq("sort_num", 1)).getId();
        // 获取最后节点的ID
        int id = nodeMapper.selectOne(new QueryWrapper<ProcessNode>().eq("p_id", processNode.getPId()).orderByDesc("sort_num").last("limit 1")).getId();

        // 目标节点ID
        int i;
        // 数据关联
        Handle selectOne = handleMapper.selectOne(new QueryWrapper<Handle>().eq("document_id", id).eq("middleman_id", userName));
        System.err.println("数据关联："+selectOne);

        // 提交审核
        if (leaveing.getNodeId() == findId) {
            System.err.println("提交审核");
            if (selectOne==null){
                handleAdd(leaveing, leaveing.getUserId(), null, processNode.getPId());
            }else {
                handleAddFanBo(leaveing.getId(),uid,null,processNode.getPId());
            }
            leaveing.setProgress("同意");
            i = routerMapper.selectOne(new QueryWrapper<ProcessRouter>().eq("current_id", leaveing.getNodeId()).eq("requirement", leaveing.getProgress())).getTargetId();
            leaveing.setNodeId(i);
            leaveing.setCareful(1);
//            // 意见 Remakes
//            Leaveing leaveOne = baseMapper.selectById(leaveing.getId());
//            leaveing.setRemarks(jsonDome(leaveOne.getRemarks(), processNode.getName(), "申请提交", leaveing.getUsername()));
//            handleAdd(leaveing,userName,null, processNode.getPId());
            if (selectOne==null){
                handleAdd(leaveing, userName, leaveing.getUserId(), processNode.getPId());
            }else {
                handleAddFanBo(leaveing.getId(),uid,userName,processNode.getPId());
            }
            baseMapper.updateById(leaveing);
            return R.ok();
        } else {
            try {
                System.err.println("获取节点信息");
                // 获取当前节点的信息
                List<ProcessRouter> processRouters = routerMapper.selectList(new QueryWrapper<ProcessRouter>().eq("current_id", leaveing.getNodeId()).eq("requirement", leaveing.getProgress()));
                for (ProcessRouter router : processRouters) {
                    if (leaveing.getNodeId() != router.getTargetId()) {
                        // 判断路由跳转条件
                        if (router.getStepToo01() != 0.0 && router.getStepToo02() != 0.0) {
                            System.err.println(router);
                            System.err.println(leaveing.getDayNumber());
                            // 如果天数小于第一个条件
                            if (Double.doubleToLongBits(router.getStepToo01()) <= Double.doubleToLongBits(leaveing.getDayNumber()) && Double.doubleToLongBits(leaveing.getDayNumber()) < Double.doubleToLongBits(router.getStepToo02())) {
                                System.err.println("天数早区间之间");
                                // 目标节点以及办理人
                                baseMapper.updateById(leaveing(leaveing, router.getTargetId(), processNode.getName(), userName));
                                if ("驳回".equals(leaveing.getProgress())){
                                        handleAddFanBo(leaveing.getId(),uid,userName,router.getPId());
                                }else {
                                        handleAdd(leaveing, userName, uid, processNode.getPId());
                                }
                                return R.ok();
                            }else {
                                if (Double.doubleToLongBits(leaveing.getDayNumber()) >= Double.doubleToLongBits(router.getStepToo02())){
                                    System.err.println("天数大于等于条件二");
                                    // 目标节点以及办理人
                                    baseMapper.updateById(leaveing(leaveing, router.getTargetId(), processNode.getName(), userName));
                                    if ("驳回".equals(leaveing.getProgress())){
                                            handleAddFanBo(leaveing.getId(),uid,userName,router.getPId());
                                    }else {
                                            handleAdd(leaveing, userName, uid, processNode.getPId());
                                    }
                                    return R.ok();
                                }else {
                                    continue ;
                                }
                            }
                        }
                        if (router.getStepToo02() != 0.0 && router.getStepToo01() == 0.0) {
                            if (Double.doubleToLongBits(router.getStepToo02()) <= Double.doubleToLongBits(leaveing.getDayNumber())) {
                                System.err.println("天数小于条件二");
                                // 目标节点以及办理人
                                baseMapper.updateById(leaveing(leaveing, router.getTargetId(), processNode.getName(), userName));
                                if ("驳回".equals(leaveing.getProgress())){
                                        handleAddFanBo(leaveing.getId(),uid,userName,router.getPId());
                                }else {
                                        handleAdd(leaveing, userName, uid, processNode.getPId());
                                }
                                return R.ok();
                            }
                        }
                        if (router.getStepToo01() != 0.0 && router.getStepToo02() == 0.0) {
                            if (Double.doubleToLongBits(router.getStepToo01()) > Double.doubleToLongBits(leaveing.getDayNumber())) {
                                System.err.println("天数小于条件一");
                                // 目标节点以及办理人
                                baseMapper.updateById(leaveing(leaveing, router.getTargetId(), processNode.getName(), userName));
                                if ("驳回".equals(leaveing.getProgress())){
                                        handleAddFanBo(leaveing.getId(),uid,userName,router.getPId());
                                }else {
                                        handleAdd(leaveing, userName, uid, processNode.getPId());
                                }
                                return R.ok();
                            }
                        }
                        if (router.getStepToo01() == 0.0 && router.getStepToo02() == 0.0) {
                            System.err.println("天数没有条件");
                            if ("驳回".equals(router.getRequirement())) {
                                baseMapper.updateById(leaveing(leaveing, router.getTargetId(), processNode.getName(), userName));
                                    handleAddFanBo(leaveing.getId(),uid,userName,router.getPId());
                                return R.ok();
                            }
                            if (router.getTargetId() == id) {
                                System.err.println("目标节点等于最后节点");
                                leaveing.setCareful(2);
                                leaveing.setNodeId(0);
                                    handleAddFanBo(leaveing.getId(),uid,null,router.getPId());
                                baseMapper.updateById(leaveing);
                                return R.ok();
                            }else {
                                // 目标节点以及办理人  userNodeAdd(router.getTargetId(), uid, processNode.getId(), id);
                                baseMapper.updateById(leaveing(leaveing, router.getTargetId(), processNode.getName(), userName));
                                if ("驳回".equals(leaveing.getProgress())){
                                        handleAddFanBo(leaveing.getId(),uid,userName,router.getPId());
                                }else {
                                        handleAdd(leaveing, userName, uid, processNode.getPId());
                                }
                                return R.ok();
                            }
                        }
                    } else {
                        leaveing.setCareful(2);
                        leaveing.setNodeId(0);
                        Leaveing leaveOne = baseMapper.selectById(leaveing.getId());
                        leaveing.setRemarks(jsonDome(leaveOne.getRemarks(), processNode.getName(), leaveing.getExplains(), userName));
                        handleAdd(leaveing, null, leaveing.getUserId(), processNode.getPId());
                        baseMapper.updateById(leaveing);
                        return R.ok();
                    }
                }
                // 获取目标节点
            } catch (Exception e) {
                e.printStackTrace();
                throw new SecurityException("该流程路由存在异常，请联系管理员！");
            }
        }
        throw new SecurityException("请联系管理员");
    }

    private void handleAddFanBo(String id, String uid, String userName, int pId) {
            Handle handle = new Handle();
            handle.setDocumentId(id)
                    .setHandleId(uid)
                    .setMiddlemanId(userName)
                    .setPId(pId);
            handleMapper.insert(handle);
    }

    /**
     * i:目标节点 uid ： 目标节点办理人 id：当前节点  id1:最后节点
     *
     * @Author: wangt
     * @Date: 2021-04-25 14:13:45
     */
    private void userNodeAdd(int i, String uid, int id, int id1, int pid) {
        if (id != id1) {
            if (EmptyUtil.isEmpty(uid)) {
                throw new SecurityException("下级审批人未空！");
            }
            QueryWrapper<NodeUser> queryWrapper = new QueryWrapper<>();
            if (!EmptyUtil.isEmpty(i)) {
                queryWrapper.eq("node_id", i);
            }
            queryWrapper.eq("user_id", uid);
            NodeUser user = nodeUserMapper.selectOne(queryWrapper);
            if (EmptyUtil.isNotEmpty(user)) {
                nodeUserMapper.deleteById(user.getId());
            }
            NodeUser nodeUser = new NodeUser();
            nodeUser.setNodeId(i);
            nodeUser.setUserId(uid);
            nodeUser.setPId(pid);
            nodeUserMapper.insert(nodeUser);
        }
    }


    private Leaveing leaveing(Leaveing leaveing, int targetId, String name, String userName) {
        leaveing.setCareful(1);
        leaveing.setNodeId(targetId);
        // 意见 Remakes
//        Leaveing leaveOne = baseMapper.selectById(leaveing.getId());
//        leaveing.setRemarks(jsonDome(leaveOne.getRemarks(), name, leaveing.getExplains(), userName));
        return leaveing;
    }


    /**
     * json转String
     *
     * @Author: wangt
     * @Date: 2021-04-20 13:58:42
     */
    private String jsonDome(String remarks, String name, String explains, String userName) {
        if (EmptyUtil.isEmpty(remarks)) {
            Map<Object, String> map = new HashMap<>(16);
            map.put("name", name);
            map.put("remakes", explains);
            map.put("userName", userName);
            JSONObject json = JSONObject.fromObject(map);
            Object[] arr = new Object[1];
            arr[0] = json;
            return Arrays.toString(arr);
        }
        List<RemakesVo> object = JSONArray.parseArray(remarks, RemakesVo.class);
        Object[] arr = new Object[object.size() + 1];
        int i = 0;
        for (RemakesVo vo : object) {
            JSONObject json = JSONObject.fromObject(vo);
            arr[i++] = json;
        }
        Map<Object, String> map = new HashMap<>(16);
        map.put("name", name);
        map.put("remakes", explains);
        map.put("userName", userName);
        JSONObject json = JSONObject.fromObject(map);
        arr[i] = json;
        return Arrays.toString(arr);
    }

    /**
     * 请假申请或者加班申请
     *
     * @Author: wangt
     * @Date: 2021-04-20 09:32:58
     */
    @Override
    public R addLeave(Leaveing leaveing) {
        if (EmptyUtil.isEmpty(leaveing.getTitle())) {
            throw new SecurityException("标题不能为空！");
        }
        if (EmptyUtil.isEmpty(leaveing.getDegree())) {
            throw new SecurityException("紧急程度不能为空！");
        }
        if (EmptyUtil.isEmpty(leaveing.getUsername())) {
            throw new SecurityException("姓名不能为空！");
        }
        if (EmptyUtil.isEmpty(leaveing.getDept())) {
            throw new SecurityException("部门不能为空！");
        }
        if (EmptyUtil.isEmpty(leaveing.getTypes())) {
            throw new SecurityException("请假类型不能为空！");
        }
        if (EmptyUtil.isEmpty(leaveing.getStartDate())) {
            throw new SecurityException("开始日期不能为空！");
        }
        if (EmptyUtil.isEmpty(leaveing.getStartTime())) {
            throw new SecurityException("开始时间不能为空！");
        }
        if (EmptyUtil.isEmpty(leaveing.getEndDate())) {
            throw new SecurityException("结束日期不能为空！");
        }
        if (EmptyUtil.isEmpty(leaveing.getEndTime())) {
            throw new SecurityException("结束时间不能为空！");
        }
        if (EmptyUtil.isEmpty(leaveing.getDayNumber())) {
            throw new SecurityException("请假天数不能为空！");
        }
        if (EmptyUtil.isEmpty(leaveing.getText())) {
            throw new SecurityException("请假原因不能为空！");
        }
        if (leaveing.getEndDate().getTime() < leaveing.getStartDate().getTime()) {
            throw new SecurityException("结束时间不能小于开始时间！");
        }
        leaveing.setId(String.valueOf(IdWorker.getId(leaveing)));
        leaveing.setCareful(0);
        leaveing.setProgress("同意");
        int id;
        try {
            if (leaveing.getState() == 1) {
                id = processMapper.selectOne(new QueryWrapper<Process>().eq("flow_name", "请假流程")).getId();
            } else {
                id = processMapper.selectOne(new QueryWrapper<Process>().eq("flow_name", "加班申请")).getId();
            }
        } catch (Exception e) {
            throw new SecurityException("没有当前操作的流程！");
        }
        try {
            leaveing.setNodeId(nodeMapper.selectOne(new QueryWrapper<ProcessNode>().eq("p_id", id).eq("sort_num", "1")).getId());
        } catch (Exception e) {
            throw new SecurityException("当前流程下没有可执行节点！");
        }
        if (leaveMapper.insert(leaveing) > 0) {
            // 代办表添加数据
            examine(leaveing, leaveing.getBanId(), null);
            return R.ok().message("添加成功！");
        }
        return R.error().message("添加失败！");
    }

    /**
     * leaing: 对象， dauId:下一节点办理人  , xianID : 当前节点办理人  id : 流程ID
     *
     * @Author: wangt
     * @Date: 2021-04-25 17:14:49
     */
    public void handleAdd(Leaveing leaveing, String daiId, String xianId, int id) {
        System.err.println("-----------------------------------------------------------------------------------"+daiId);
        handleMapper.delete(new QueryWrapper<Handle>().eq("document_id", leaveing.getId()).eq("middleman_id", daiId));
        handleMapper.delete(new QueryWrapper<Handle>().eq("document_id", leaveing.getId()).eq("handle_id", daiId));
        Handle handle = new Handle();
        if ("null".equals(daiId) || "undefined".equals(daiId) || daiId==null) {
            handle.setMiddlemanId(leaveing.getUserId());
        } else {
            handle.setMiddlemanId(daiId);
        }
        handle.setDocumentId(leaveing.getId())
                .setHandleId(xianId)
                .setNId(leaveing.getNodeId())
                .setPId(id);

        handleMapper.insert(handle);
    }

    /**
     * 待审批
     *
     * @Author: wangt
     * @Date: 2021-04-18 15:23:43
     */
    @Override
    public IPage<Leaveing> selectPage(Page<Leaveing> pageParam, Object o) {
        QueryWrapper<Leaveing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("careful", "0");
        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 动态展示待审批
     *
     * @Author: wangt
     * @Date: 2021-04-18 16:59:55
     */
    @Override
    public R selectPage1(int page, int limit, Object o, String id) {
        // 代办
        List<Leaveing> list1 = new ArrayList<>();
        // 获取当前登录用户所对应要进行审批处理的数据
        List<Leaveing> list = baseMapper.selectList(new QueryWrapper<Leaveing>()
                .eq("user_id", id).or().eq("ban_id", id)
                .or().eq("fen_id", id).or().eq("zhu_id", id));
        if (list == null) {
            return R.ok().data("rows", null).data("total", 0);
        }
        for (Leaveing leaveing : list) {
            // 代办数量
            Integer integer1 = handleMapper.selectCount(new QueryWrapper<Handle>().eq("document_id", leaveing.getId()).eq("middleman_id", id));
            // 已办数量
            Integer integer2 = handleMapper.selectCount(new QueryWrapper<Handle>().eq("document_id", leaveing.getId()).eq("handle_id", id));
            // 代办
            if (integer1 > integer2) {
                list1.add(leaveing);
            }
//            if (id.equals(leaveing.getUserId())){
//                list1.add(leaveing);
//            }else {
//                if (integer1>integer2) {
//                    list1.add(leaveing);
//                }
//            }
            // 已办
//            if (id.equals(leaveing.getUserId())) {
//                if (leaveing.getNodeId()==0 && "同意".equals(leaveing.getProgress())){
//                    list2.add(leaveing);
//                }
//            }else {
//                if (integer1.equals(integer2)) {
//                    list2.add(leaveing);
//                }
//            }
        }
        return R.ok().data("rows", page(list1, limit, page)).data("total", list1.size());
    }

    /**
     * 循环截取某页列表进行分页
     *
     * @param dataList    分页数据
     * @param pageSize    页面大小
     * @param currentPage 当前页面
     */
    public static List<Leaveing> page(List<Leaveing> dataList, int pageSize, int currentPage) {
        List<Leaveing> currentPageList = new ArrayList<>();
        if (dataList != null && dataList.size() > 0) {
            int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0);
            for (int i = 0; i < pageSize && i < dataList.size() - currIdx; i++) {
                currentPageList.add(dataList.get(currIdx + i));
            }
        }
        return currentPageList;
    }

    /**
     * 展示已审批
     *
     * @Author: wangt
     * @Date: 2021-04-22 10:07:44
     */
    @Override
    public R stateList1(int page, int limit, LeaveQueryVo leaveQueryVo, String id) {
        // 已办
        List<Leaveing> list2 = new ArrayList<>();
        QueryWrapper<Leaveing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id).or().eq("ban_id", id)
                .or().eq("fen_id", id).or().eq("zhu_id", id);
        if (!EmptyUtil.isEmpty(leaveQueryVo.getTitle())) {
            queryWrapper.like("title", leaveQueryVo.getTitle());
        }
        if (!EmptyUtil.isEmpty(leaveQueryVo.getUsername())) {
            queryWrapper.like("username", leaveQueryVo.getUsername());
        }
        if (!EmptyUtil.isEmpty(leaveQueryVo.getStartDate())) {
            queryWrapper.ge("start_date", leaveQueryVo.getStartDate());
            queryWrapper.le("start_date", leaveQueryVo.getStartDate());
        }
        List<Leaveing> leaveingList = baseMapper.selectList(queryWrapper);
        if (leaveingList == null) {
            return R.ok().data("rows", null).data("total", 0);
        }
        for (Leaveing leaveing : leaveingList) {
            // 代办数量
            Integer integer1 = handleMapper.selectCount(new QueryWrapper<Handle>().eq("document_id", leaveing.getId()).eq("middleman_id", id));
            // 已办数量
            Integer integer2 = handleMapper.selectCount(new QueryWrapper<Handle>().eq("document_id", leaveing.getId()).eq("handle_id", id));
            // 已办
            if (integer2 >= integer1) {
                list2.add(leaveing);
            }
        }
        return R.ok().data("rows", page(list2, limit, page)).data("total", list2.size());
    }

    /**
     * ListVo封装
     *
     * @Author: wangt
     * @Date: 2021-04-20 15:19:48
     */
    private LeaveListVo listVo(Leaveing leaveing) {
        LeaveListVo vo = new LeaveListVo();
        vo.setId(leaveing.getId());
        vo.setTitle(leaveing.getTitle());
        vo.setText(leaveing.getText());
        vo.setUsername(leaveing.getUsername());
        vo.setStartDate(leaveing.getStartDate());
        vo.setEndDate(leaveing.getEndDate());
        vo.setState(leaveing.getState());
        if (!EmptyUtil.isEmpty(leaveing.getRemarks())) {
            List<RemakesVo> remakesVos = new ArrayList<>(JSONArray.parseArray(leaveing.getRemarks(), RemakesVo.class));
            vo.setRemarks(remakesVos);
        }
        return vo;
    }

    /**
     * 分页封装
     *
     * @Author: wangt
     * @Date: 2021-04-20 15:19:39
     */
    private PaginationVo<Leaveing> vo(List<Leaveing> tests) {
        PaginationVo<Leaveing> vo = new PaginationVo<>();
        PageInfo<Leaveing> pageInfo = new PageInfo<>(tests);
        vo.setTotal(Integer.parseInt(String.valueOf(pageInfo.getTotal())));
        vo.setDataList(tests);
        return vo;
    }

    /**
     * 出勤率  出勤人数÷应出勤人数×100%
     *
     * @Author: wangt
     * @Date: 2021-04-21 20:51:01
     */
    @Override
    public R chuQin(LeaveingVo leaveingVo) {
        // 总人数
        Integer integer01 = userMapper.selectCount(null);
        // 请假人数
        int integer02 = leaNum(leaveingVo);
        if (integer01 != 0 && integer02 != 0) {
            double num1 = integer01;
            NumberFormat formatter = new DecimalFormat("0.00");
            double x = (num1 - (double) integer02) / num1 * 100;
            return R.ok().data("chuQin", formatter.format(x)).data("userNum", integer01);
        }
        if (integer02 == 0) {
            return R.ok().data("chuQin", "100").data("userNum", integer01);
        }
        return R.ok().data("chuQin", "00.00").data("userNum", integer01);
    }

    /**
     * 已审批
     *
     * @Author: wangt
     * @Date: 2021-04-18 15:24:00
     */
    @Override
    public IPage<Leaveing> stateList(Page<Leaveing> pageParam, LeaveQueryVo leaveQueryVo) {
        if (leaveQueryVo == null) {
            QueryWrapper<Leaveing> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("careful", "2");
            queryWrapper.eq("progress", "同意");
            return baseMapper.selectPage(pageParam, queryWrapper);
        }
        QueryWrapper<Leaveing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("careful", "2");
        queryWrapper.eq("progress", "同意");
        if (!EmptyUtil.isEmpty(leaveQueryVo.getTitle())) {
            queryWrapper.like("title", leaveQueryVo.getTitle());
        }
        if (!EmptyUtil.isEmpty(leaveQueryVo.getUsername())) {
            queryWrapper.like("username", leaveQueryVo.getUsername());
        }
        if (!EmptyUtil.isEmpty(leaveQueryVo.getStartDate())) {
            queryWrapper.ge("start_date", leaveQueryVo.getStartDate());
            queryWrapper.le("start_date", leaveQueryVo.getStartDate());
        }
        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 根据时间戳获取请假人数
     *
     * @Author: wangt
     * @Date: 2021-04-18 15:24:11
     */
    @Override
    public int leaNum(LeaveingVo leaveingVo) {
        QueryWrapper<Leaveing> queryWrapper = new QueryWrapper<>();
        String startDate = leaveingVo.getStartDate();
        String endDate = leaveingVo.getEndDate();
        queryWrapper.eq("state", 1);
        queryWrapper.eq("careful", 2);
        queryWrapper.eq("progress", "同意");
        if (!EmptyUtil.isEmpty(startDate)) {
            queryWrapper.ge("start_date", startDate);
        }
        if (!EmptyUtil.isEmpty(endDate)) {
            queryWrapper.le("start_date", endDate);
        }
        return baseMapper.selectCount(queryWrapper);
    }

    /**
     * 加班
     *
     * @param leaveingVo 筛选对象
     * @return int
     */
    @Override
    public int pubNum(LeaveingVo leaveingVo) {
        QueryWrapper<Leaveing> queryWrapper = new QueryWrapper<>();
        String startDate = leaveingVo.getStartDate();
        String endDate = leaveingVo.getEndDate();
        queryWrapper.eq("state", "0");
        queryWrapper.eq("careful", "2");
        queryWrapper.eq("progress", "同意");
        if (!EmptyUtil.isEmpty(startDate)) {
            queryWrapper.ge("start_date", startDate);
        }
        if (!EmptyUtil.isEmpty(endDate)) {
            queryWrapper.le("start_date", endDate);
        }
        return baseMapper.selectCount(queryWrapper);
    }

    /**
     * 类型
     *
     * @param sta        类型
     * @param leaveingVo 时间对象
     * @return 对象
     */
    private Wrapper<Leaveing> queryWrapper01(String sta, LeaveingVo leaveingVo) {
        QueryWrapper<Leaveing> queryWrapper = new QueryWrapper<>();
        String startDate = leaveingVo.getStartDate();
        String endDate = leaveingVo.getEndDate();
        queryWrapper.eq("state", 1);
        queryWrapper.eq("types", sta);
        queryWrapper.eq("careful", 2);
        queryWrapper.eq("progress", "同意");
        if (!EmptyUtil.isEmpty(startDate)) {
            queryWrapper.ge("start_date", startDate);
        }
        if (!EmptyUtil.isEmpty(endDate)) {
            queryWrapper.le("start_date", endDate);
        }
        return queryWrapper;
    }

    /**
     * 事假
     *
     * @Author: wangt
     * @Date: 2021-04-18 16:33:09
     */
    @Override
    public int findShi(LeaveingVo leaveingVo) {
        return baseMapper.selectCount(queryWrapper01("事假", leaveingVo));
    }

    /**
     * 年假
     *
     * @Author: wangt
     * @Date: 2021-04-18 16:33:18
     */
    @Override
    public int findYear(LeaveingVo leaveingVo) {
        return baseMapper.selectCount(queryWrapper01("年假", leaveingVo));
    }

    /**
     * 病假
     *
     * @Author: wangt
     * @Date: 2021-04-18 16:33:28
     */
    @Override
    public int findBing(LeaveingVo leaveingVo) {
        return baseMapper.selectCount(queryWrapper01("病假", leaveingVo));
    }

    /**
     * 根据ID获取请假
     *
     * @Author: wangt
     * @Date: 2021-04-18 16:33:47
     */
    @Override
    public Leaveing findById(String id) {
        return baseMapper.selectById(id);
    }

    /**
     * 其他
     *
     * @Author: wangt
     * @Date: 2021-04-18 16:33:35
     */
    @Override
    public int findQiTa(LeaveingVo leaveingVo) {
        return baseMapper.selectCount(queryWrapper01("其它", leaveingVo));
    }

    /**
     * 婚假
     *
     * @Author: wangt
     * @Date: 2021-04-23 15:28:09
     */
    public int findHunJia(LeaveingVo leaveingVo) {
        return baseMapper.selectCount(queryWrapper01("婚假", leaveingVo));
    }

    /**
     * 丧假
     *
     * @Author: wangt
     * @Date: 2021-04-23 15:28:42
     */
    public int findSangJia(LeaveingVo leaveingVo) {
        return baseMapper.selectCount(queryWrapper01("丧假", leaveingVo));
    }

    /**
     * 护理假
     *
     * @Author: wangt
     * @Date: 2021-04-23 15:29:18
     */
    public int findHuLi(LeaveingVo leaveingVo) {
        return baseMapper.selectCount(queryWrapper01("护理假", leaveingVo));
    }

    /**
     * 探亲假
     *
     * @Author: wangt
     * @Date: 2021-04-23 15:29:24
     */
    public int findTanQin(LeaveingVo leaveingVo) {
        return baseMapper.selectCount(queryWrapper01("探亲假", leaveingVo));
    }

    /**
     * 根据ID获取加班
     *
     * @Author: wangt
     * @Date: 2021-04-18 16:33:59
     */
    @Override
    public Leaveing findByPub(String id) {
        QueryWrapper<Leaveing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", "0");
        queryWrapper.eq("id", id);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 柱状图
     *
     * @Author: wangt
     * @Date: 2021-04-18 16:34:12
     */
    @Override
    public R stateZhu(LeaveingVo leaveingVo) {
        List<LeaveStateVo> list = new ArrayList<>();
        int shi = findShi(leaveingVo);
        LeaveStateVo leaveStateVo = new LeaveStateVo();
        leaveStateVo.setValue(shi);
        leaveStateVo.setName("事假");
        list.add(leaveStateVo);
        int findYear = findYear(leaveingVo);
        LeaveStateVo leaveStateVo1 = new LeaveStateVo();
        leaveStateVo1.setValue(findYear);
        leaveStateVo1.setName("年假");
        list.add(leaveStateVo1);
        int findBing = findBing(leaveingVo);
        LeaveStateVo leaveStateVo2 = new LeaveStateVo();
        leaveStateVo2.setValue(findBing);
        leaveStateVo2.setName("病假");
        list.add(leaveStateVo2);
        int findQiTa = findQiTa(leaveingVo);
        LeaveStateVo leaveStateVo3 = new LeaveStateVo();
        leaveStateVo3.setValue(findQiTa);
        leaveStateVo3.setName("其它");
        list.add(leaveStateVo3);
        int hunJia = findHunJia(leaveingVo);
        LeaveStateVo leaveStateVo4 = new LeaveStateVo();
        leaveStateVo4.setValue(hunJia);
        leaveStateVo4.setName("婚假");
        list.add(leaveStateVo4);
        int sangJia = findSangJia(leaveingVo);
        LeaveStateVo leaveStateVo5 = new LeaveStateVo();
        leaveStateVo5.setValue(sangJia);
        leaveStateVo5.setName("丧假");
        list.add(leaveStateVo5);
        int huLi = findHuLi(leaveingVo);
        LeaveStateVo leaveStateVo6 = new LeaveStateVo();
        leaveStateVo6.setValue(huLi);
        leaveStateVo6.setName("护理假");
        list.add(leaveStateVo6);
        int tanQin = findTanQin(leaveingVo);
        LeaveStateVo leaveStateVo7 = new LeaveStateVo();
        leaveStateVo7.setValue(tanQin);
        leaveStateVo7.setName("探亲假");
        list.add(leaveStateVo7);
        return R.ok().message("成功！").data("list", list);
    }


}
